package org.gd.hellojaxb.privat24.paninfo;

import org.gd.hellojaxb.utils.Marshall;
import org.gd.hellojaxb.utils.MarshallProperties;

import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@XmlRootElement(name = "doc")
@XmlAccessorType(XmlAccessType.FIELD)
public class PanInfoRequest {

    @XmlElement(name = "r")
    private final List<PanInfoRequestCard> cards;

    public PanInfoRequest() {
        super();
        cards = new ArrayList<PanInfoRequestCard>();
    }

    public PanInfoRequest(String sid, String...cards) {
        this();
        add(sid, cards);
    }

    @SuppressWarnings("unused")
    public List<PanInfoRequestCard> getCards() {
        return cards;
    }

    public PanInfoRequest add(String sid, String...cards) {
        if(sid != null && cards != null) {
            for(String card: cards) {
                if(card != null) {
                    this.cards.add(new PanInfoRequestCard(sid, card));
                }
            }
        }
        return this;
    }

    @Override
    public String toString() {
        try {
            return Marshall.getInstance().marshal(this, MarshallProperties.WITHOUT_XML_HEADER);
        } catch (Throwable e) {
            return super.toString();
        }
    }

    public static void main(String...args) throws JAXBException, IOException {
        System.out.println(Marshall.getInstance().marshal(
                new PanInfoRequest("sid", "1").add("sid", "2"),
                MarshallProperties.FORMATTED_OUTPUT));
    }
}
